package guestbook.ui;

import guestbook.model.GuestbookEntry;
import guestbook.rest.ApiGuestbookEntry;

import java.util.List;

import org.fusesource.restygwt.client.Method;
import org.fusesource.restygwt.client.MethodCallback;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;

public class MainPageView extends ViewImpl implements MainPagePresenter.MyView {

	final ApiGuestbookEntry apiGuestbookentry;

	final VerticalPanel mainPanel = new VerticalPanel();
	final VerticalPanel panelWithCommentspanelWithComments = new VerticalPanel();

	final TextArea guestbookEntryTextArea = new TextArea();
	final TextArea authorTextArea = new TextArea();
	final Button addCommentButton = new Button("Add comment");

	@Inject
	public MainPageView(final ApiGuestbookEntry apiGuestbookentry) {

		this.apiGuestbookentry = apiGuestbookentry;

		layoutPage();

		// bad design => should be in presenter...
		addClickHandlers();

		// init page
		doReloadEntries();

	}

	public void addClickHandlers() {

		addCommentButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				doAddComment();

			}
		});

	}

	public void doAddComment() {

		final GuestbookEntry guestbookEntry = new GuestbookEntry();
		guestbookEntry.author = authorTextArea.getText();
		guestbookEntry.text = guestbookEntryTextArea.getText();

		apiGuestbookentry.addGuestbookEntry(guestbookEntry,
				new MethodCallback<Void>() {

					@Override
					public void onSuccess(Method method, Void response) {
						Window.alert("added :) ");
						doReloadEntries();

					}

					@Override
					public void onFailure(Method method, Throwable exception) {
						Window.alert("failure");

					}
				});

	}

	public void doReloadEntries() {

		apiGuestbookentry
				.getGuestbookEntrys(new MethodCallback<List<GuestbookEntry>>() {

					@Override
					public void onSuccess(Method method,
							List<GuestbookEntry> response) {

						panelWithCommentspanelWithComments.clear();

						for (GuestbookEntry guestbookEntry : response) {

							panelWithCommentspanelWithComments.add(new HTML(
									"<h2>" + guestbookEntry.author + "</h2>"));

							panelWithCommentspanelWithComments.add(new HTML(
									"<p>" + guestbookEntry.text + "</p>"));

						}
					}

					@Override
					public void onFailure(Method method, Throwable exception) {
						Window.alert("failure");

					}
				});

	}

	/**
	 * in reality better with ui binder...
	 */
	public void layoutPage() {

		VerticalPanel headerPanel = new VerticalPanel();
		mainPanel.add(headerPanel);
		headerPanel.add(new HTML(
				"<h1>The very best guestbook in the world</h1>"));

		HorizontalPanel guestbookEntryPanel = new HorizontalPanel();
		guestbookEntryPanel.add(new Label("Say something: "));
		guestbookEntryPanel.add(guestbookEntryTextArea);
		headerPanel.add(guestbookEntryPanel);

		HorizontalPanel authorPanel = new HorizontalPanel();
		authorPanel.add(new Label("Your name: "));
		authorPanel.add(authorTextArea);
		headerPanel.add(authorPanel);

		headerPanel.add(addCommentButton);

		mainPanel.add(new HTML("<h2>Previous entries</h2>"));
		mainPanel.add(panelWithCommentspanelWithComments);

	}

	@Override
	public Widget asWidget() {
		return mainPanel;
	}
}
